<?php

class Controller_User_Signup
{
	function execute($request)
	{
		$form = new Form_User_Signup();

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$data = $form -> getValues();
			$email = $data['email'];
			$user = Mongo_Document::factory('user') -> set('email', $data['email']) -> set('displayname', $data['displayname']);
			$user -> set('_id', $user -> db() -> get_auto_increment());
            
			$user -> created_at = time();
			$user -> is_verified = 1;
			$user -> is_approved = 1;
			$user -> is_active = 1;
			$user -> level_id = 5;

			$password = $data['password'];

			$user -> setPassword($password);

			$user -> save($safe = TRUE);

			$code = Text::random(32);

			Mongo_Database::instance() -> selectCollection('user_verify') -> insert(array(
				'_id' => $code,
				'user_id' => $user -> _id,
				'timestamp' => time(),
			), 1);

			/*
			 * $mailer = Mailer::inst() -> addReceipient($data['email'],
			 * $data['displayname']) -> setSubject('email kích hoạt tài khoản') ->
			 * setMessage('mail/user/verification.tpl', $data = array(
			 'displayname' => $data['displayname'],
			 'email' => $data['email'],
			 'password' => $password,
			 'url' => Agilet::getUrl('user/verify/?code=' . $code),
			 )) -> send();
			 */

			Agilet::redirect('user/signup/success');
		}

		$request -> response = View::render($data = array('form' => $form), 'controller/user/signup.tpl');
	}

}
